﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Mbs.Threading
{
    [Serializable]
    public class ResetEvent
    {
        private volatile bool state;
        private readonly object lockObj = new object();

        public ResetEvent(bool initialState)
        {
            state = initialState;
        }


        public bool Wait()
        {
            lock (lockObj)
            {
                if (state)
                    return true;
                return Monitor.Wait(lockObj);
            }
        }

        public void Set()
        {
            lock (lockObj)
            {
                Monitor.PulseAll(lockObj);
                state = true;
            }
        }

        public void Reset()
        {
            lock (lockObj)
                state = false;
        }
    }
}
